﻿using System;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace MvvmFx.Common.Helpers
{
    public static class TransitionHelper
    {
        #region Fade In/Out
        
        private const double DEFAULT_FADE_OUT_DURATION = 400;
        private const double DEFAULT_FADE_IN_DURATION = 400;

        public static void FadeIn(UserControl fadeIn)
        {
            TimeSpan duration = TimeSpan.FromMilliseconds(DEFAULT_FADE_IN_DURATION);

            Storyboard story = new Storyboard();
            story.FillBehavior = FillBehavior.HoldEnd;

            AnimationHelper.CreateAnimation(
                story,
                fadeIn,
                "Opacity",
                new DoubleAnimation
                    {
                        Duration = duration,
                        From = 0,
                        To = 1
                    });

            story.Completed += delegate
                                   {
                                       fadeIn.Opacity = 1;
                                   };

            story.Begin();
        }

        public static void FadeOut(UserControl fadeIn, EventHandler callback)
        {
            TimeSpan duration = TimeSpan.FromMilliseconds(DEFAULT_FADE_OUT_DURATION);

            Storyboard story = new Storyboard();
            story.FillBehavior = FillBehavior.HoldEnd;

            AnimationHelper.CreateAnimation(
                story,
                fadeIn,
                "Opacity",
                new DoubleAnimation
                    {
                        Duration = duration,
                        From = 1,
                        To = 0
                    });

            story.Completed += delegate
                                   {
                                       fadeIn.Opacity = 0;
                                       callback(null, EventArgs.Empty);
                                   };

            story.Begin();
        }

        #endregion

        #region Slide

        private const double DEFAULT_SLIDE_DURATION = 600;

        public static void Slide(UserControl control, double to)
        {
            control.Visibility = System.Windows.Visibility.Visible;

            TimeSpan duration = TimeSpan.FromMilliseconds(DEFAULT_SLIDE_DURATION);

            Storyboard story = new Storyboard();
            story.FillBehavior = FillBehavior.HoldEnd;

            AnimationHelper.CreateAnimation(
                story,
                control,
                "(Canvas.Left)",
                new DoubleAnimation
                    {
                        Duration = duration,
                        To = to,
                        DecelerationRatio = 1
                    });

            story.Completed += delegate
                                   {
                                       Canvas.SetLeft(control, to);
                                   };

            story.Begin();
        }

        #endregion
    }
}